package com.lanhai.fnstore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.lanhai.fnstore.cache.AgvCache;
import com.lanhai.fnstore.cache.AgvItem;
import com.lanhai.fnstore.cache.EstimateCache;
import com.lanhai.fnstore.cache.MountCache;
import com.lanhai.fnstore.cache.ParkCache;
import com.lanhai.fnstore.cache.ParkItem;
import com.lanhai.fnstore.cache.ParkRelationCache;
import com.lanhai.fnstore.cache.ParkRelationItem;
import com.lanhai.fnstore.cache.ParkTypeEnum;
import com.lanhai.fnstore.cache.PowerCache;
import com.lanhai.fnstore.cache.PowerItem;
import com.lanhai.fnstore.cache.RoadCache;
import com.lanhai.fnstore.cache.RoadItem;
import com.lanhai.fnstore.cache.StationGroupCache;
import com.lanhai.fnstore.cache.StationGroupItem;
import com.lanhai.fnstore.cache.StationPark;
import com.lanhai.fnstore.cache.TranfficArea;
import com.lanhai.fnstore.cache.TranfficAreaCache;
import com.lanhai.fnstore.localstore.AgvLocalBean;
import com.lanhai.fnstore.localstore.AgvMountLocalBean;
import com.lanhai.fnstore.localstore.AgvMountTools;
import com.lanhai.fnstore.localstore.AgvTools;
import com.lanhai.fnstore.localstore.EstimateLocalBean;
import com.lanhai.fnstore.localstore.EstimateTools;
import com.lanhai.fnstore.localstore.ParkLocalBean;
import com.lanhai.fnstore.localstore.ParkTools;
import com.lanhai.fnstore.localstore.PowerLocalBean;
import com.lanhai.fnstore.localstore.PowerLocalTools;
import com.lanhai.fnstore.localstore.RoadLocalTools;
import com.lanhai.fnstore.localstore.StationGroupLocalBean;
import com.lanhai.fnstore.localstore.StationGroupTools;
import com.lanhai.fnstore.localstore.TranfficAreaLocalBean;
import com.lanhai.fnstore.localstore.TranfficAreaTools;

public class LoadDB {
	/**
	 * 加载数据库数据
	 * 
	 * @throws Exception
	 */
	 static void loadDbData(Config cnf) throws Exception {
		 DBHelper helper=new DBHelper(cnf.getConnStr(),cnf.getConnUser(),cnf.getConnPwd());
		////  路线
		 List<RoadItem> roadList=helper.getRoad();
		 if(roadList.size()>0) {
			 //路线和工序的映射
			 for(RoadItem road:roadList) {
				  RoadCache.add(road.getRoadId(),  road.getLv());
					  
			 }
		 } 
		 //上锁情况
		 List<Integer> lockList=RoadLocalTools.loadFile();
		 if(lockList.size()>0) {
			 for(Integer si:lockList) {
				 RoadCache.lockRoad(si, true);
			 }
		 }
		 
		 
		 ///工序分组及对应的选择策略
		 helper.initProcessSelectType();
		
		////  加载小车数据及缓存
		List<AgvItem> agvList=helper.getAgvList();
		List<AgvLocalBean> agvbeanList=AgvTools.loadFile();
		if(agvbeanList.size()>0) {
			for(AgvItem t1:agvList) {
				for(AgvLocalBean t2:agvbeanList) {
					if(t1.getAgvId()==t2.getAgvId()) {
						t1.loadDB(t2); 
						break;
					}
				}
			}
		}
		AgvCache.addList(agvList);
	 
		
		
		//// 加载预判断点野车列表 
		List<EstimateLocalBean> roadbeanList=EstimateTools.loadFile();
		if(roadbeanList.size()>0) {
			for( EstimateLocalBean rc:roadbeanList) {
				EstimateCache.add(rc.getParkNo(),rc.getStopAgv());
			}
		}
	 
		
		//// 加载装卸情况 MountCache 
		List<AgvMountLocalBean> mountList=AgvMountTools.loadFile();
		MountCache.add(mountList); 
	  
		
		//交管
		initTraffic(helper);
		 
		
		//停泊点
		initPark(helper,roadList);
		 
		
		//充电桩
		List<PowerItem> powList=helper.getPowerRole();
		if(powList.size()>0) {
			List<PowerLocalBean> beanlist=PowerLocalTools.loadFile();
			if(beanlist.size()>0) {
				for(PowerItem s1:powList) {
					for(PowerLocalBean bean:beanlist) {
						if(s1.getParkNo()==bean.getParkNo()) {
							s1.loadDB(bean);
							break;
						}
					}
				}
			} 
		} 
		PowerCache.add(powList);
		
	 }
	 
	 
	 
	 
	 
	 
	 /**
	  * 初始化交管区域
	 * @param helper
	 * @throws Exception
	 */
	static void initTraffic(DBHelper helper) throws Exception {
			List<TranfficArea> areaList=helper.getTranfficAreaList();
			List<TranfficAreaLocalBean> areabeanList=TranfficAreaTools.loadFile();
			if(areaList.size()>0) {
				for(TranfficArea t1:areaList) {
					for(TranfficAreaLocalBean t2:areabeanList) {
						if(t1.getAreaId()==t2.getAreaId()) {
							t1.loadDB(t2); 
							break;
						}
					}
				}
			}
			TranfficAreaCache.addList(areaList);
	 }
	 
	 /**
	  * 初始化停泊点及其相关的数据
	 * @param helper
	 * @param roadList
	 * @throws Exception
	 */
	static void initPark( DBHelper helper ,List<RoadItem> roadList) throws Exception {
	////  加载停泊位( 停泊位，left 对接设备）
		 
			List<ParkItem> parkList=helper.getParkList();
			//缓存的对接设备装卸情况
			List<ParkLocalBean> parkbeanList=ParkTools.loadFile();
			if(parkList.size()>0) {
				for(ParkItem t1:parkList) {
					for(ParkLocalBean t2:parkbeanList) {
						if(t1.getParkNo()==t2.getParkNo()) {
							t1.loadDB(t2); 
							break;
						}
					}
				}
			}
			
			//路线点和停泊点之间的关联
			if (parkList.size() > 0 && roadList.size() > 0) { 
				for (RoadItem roaditem : roadList) {
					for (ParkItem parkitem : parkList) {
						if (roaditem.getSourceParkNo() == parkitem.getParkNo()&&parkitem.getSourceRoad()==0) {
							parkitem.setSourceRoad(roaditem.getRoadId());
						}
						
						if (roaditem.getTargetParkNo() == parkitem.getParkNo()&&parkitem.getTargetRoad()==0) {
							parkitem.setTargetRoad(roaditem.getRoadId());
						}
					}
				}
			}
			
			//待命点 ReadyDotCache
			List<ParkItem> rList=new ArrayList<ParkItem>(); //记录所有的装卸分配点
			List<ParkItem> transList=new ArrayList<ParkItem>(); //记录所有的装卸分配点
			for (ParkItem parkitem : parkList) {
				if(parkitem.getType()==ParkTypeEnum.Decide) { 
					rList.add(parkitem); 
				}else if(parkitem.getType()==ParkTypeEnum.Trans)
				{
					transList.add(parkitem);
				}
			}
			 
			// 预判断位 和 装卸分配点 的关系
			List<ParkRelationItem> rr1=helper.getEstimateParkRelaction();
			ParkRelationCache.addEstimate(rr1);
			 
			// 装卸分配点 和 释放点  的关系
			List<ParkRelationItem> rr2=new ArrayList<ParkRelationItem>();
			for (ParkItem parkitem : rList) {
				
				ParkRelationItem pi=new ParkRelationItem();
				pi.setStartdot(parkitem.getParkNo());
				pi.setEnddot(parkitem.getReleaseDot());
				rr2.add(pi); 
			}
			ParkRelationCache.addReleaseList(rr2);
			 
			
			 
			//设备分组
			if(parkList.size()>0) {
				List<StationGroupLocalBean> sgList=StationGroupTools.loadFile();
				
				//加载设备分组
				//StationGroupCache
				List<StationGroupItem> stationList=helper.getStationGroup(); //from db
				List<StationGroupItem> stmpList=new ArrayList<StationGroupItem>(); //temp
				for(StationGroupItem sgroup: stationList) { 
					List<StationPark> sublist=new ArrayList<StationPark>();
					 
					for(ParkItem item:transList) {
						//关联的点位
						if(item.getStationGroup().equals(sgroup.getStationGroup())) {
							StationPark sg=new StationPark();
							sg.setParkNo(item.getParkNo()); 
							sg.setDisable(item.isStationDisable());
							sg.setProcessGroup(item.getProcessGroup());
							sg.setCmdSign(item.getCmdSign());
							sublist.add(sg);
							 
						}
					}  
					sgroup.setParkList(sublist);
					
					
					sgroup.setLocking(false); 
					//场景恢复
					for(StationGroupLocalBean gl:sgList) {
						if(gl.getId()==sgroup.getId() ) { 
							sgroup.setLocking(gl.isLocking());
							break;
						}
					} 
					stmpList.add(sgroup);
				} 
				 
				Collections.sort(stmpList, new Comparator<StationGroupItem>() {

					@Override
					public int compare(StationGroupItem o1, StationGroupItem o2) {
						// o1-o2是升序 
						 return o1.getWeight() - o2.getWeight();
						 
					}
				});
			 
				StationGroupCache.addAll(stmpList);
			}  
			
			 
			
			ParkCache.addList(parkList);
			
			////  加载停泊位  end
			
			 
	 }
}
